"""
需求：请求IHRM项目的登录接口
url: http://ihrm-test.itheima.net/api/sys/login
请求方式：POST
请求体-json提交：
    登陆成功：mobile: 13800000002, password: 123456
    用户不存在：mobile: 13800002332, password: 123456
    密码错误：mobile: 13800000002, password: 123abc
"""
import unittest

import requests
from htmltestreport import HTMLTestReport


class TestIHRMLogin(unittest.TestCase):
    url = 'http://ihrm-test.itheima.net/api/sys/login'

    def test01_login_ok(self):  # test后跟数字，按顺序执行
        body_json = {
            'mobile': '13800000002',
            'password': '123456'
        }

        response = requests.post(TestIHRMLogin.url, json=body_json)
        resp_data = response.json()

        self.assertEqual(200, response.status_code)
        self.assertTrue(resp_data.get('success'))
        self.assertEqual(10000, resp_data.get('code'))
        self.assertIn('成功', resp_data.get('message'))

    def test02_user_err(self):
        body_json = {
            'mobile': '13800002332',
            'password': '123456'
        }

        response = requests.post(TestIHRMLogin.url, json=body_json)
        resp_data = response.json()

        self.assertEqual(200, response.status_code)
        self.assertFalse(resp_data.get('success'))
        self.assertEqual(20001, resp_data.get('code'))
        self.assertIn('错误', resp_data.get('message'))

    def test03_pwd_err(self):
        body_json = {
            'mobile': '13800000002',
            'password': '123abc'
        }

        response = requests.post(TestIHRMLogin.url, json=body_json)
        resp_data = response.json()

        self.assertEqual(200, response.status_code)
        self.assertFalse(resp_data.get('success'))
        self.assertEqual(20001, resp_data.get('code'))
        self.assertIn('错误', resp_data.get('message'))

if __name__ == '__main__':
    suite = unittest.defaultTestLoader.discover('./', 'hm09*')
    HTMLTestReport('iHRM_login.html').run(suite)